package com.person.leetcode.editor.cn;

import java.util.concurrent.atomic.AtomicInteger;

/**
  * 1114.按序打印
  * 2022-04-07 16:42:31
 * TODO
**/
public class Sequence_1114_PrintInOrder {
    public static void main(String[] args) {
        Foo solution = new Sequence_1114_PrintInOrder().new Foo();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Foo {

        private AtomicInteger firstJobDone = new AtomicInteger(0);
        private AtomicInteger secondJobDone = new AtomicInteger(0);

        public Foo() {}

        public void first(Runnable printFirst) throws InterruptedException {
            // printFirst.run() outputs "first".
            printFirst.run();
            // mark the first job as done, by increasing its count.
            firstJobDone.incrementAndGet();
        }

        public void second(Runnable printSecond) throws InterruptedException {
            while (firstJobDone.get() != 1) {
                // waiting for the first job to be done.
            }
            // printSecond.run() outputs "second".
            printSecond.run();
            // mark the second as done, by increasing its count.
            secondJobDone.incrementAndGet();
        }

        public void third(Runnable printThird) throws InterruptedException {
            while (secondJobDone.get() != 1) {
                // waiting for the second job to be done.
            }
            // printThird.run() outputs "third".
            printThird.run();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
